package zhangtao.bwie.com.yuekao2;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;

import Bean.CarBean;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class MyAdapter_Father extends RecyclerView.Adapter<MyAdapter_Father.ViewHolder>{
    private Context context;
    private List<CarBean.DataBean> alist;
    private MyAdapter_Chiled myAdapter_chiled;

    public MyAdapter_Father(Cars_act cars_act, List<CarBean.DataBean> shop_data) {
        this.context = cars_act;
        this.alist = shop_data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View father = View.inflate(context, R.layout.father_item, null);
        ViewHolder father_vh = new ViewHolder(father);
        return father_vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.father_title.setText(alist.get(position).getSellerName());
        myAdapter_chiled = new MyAdapter_Chiled(context,alist.get(position).getList());
        LinearLayoutManager manager = new LinearLayoutManager(context);
        holder.chiled_recy.setLayoutManager(manager);
        holder.chiled_recy.setAdapter(myAdapter_chiled);
        holder.father_che.setOnCheckedChangeListener(null);
        holder.father_che.setChecked(alist.get(position).ischeck());
        holder.father_che.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {
                if(mCallBack != null) {
                    mCallBack.OnCheckLinstener_Father(b,position);
                }
                myAdapter_chiled.setOnDelete(new MyAdapter_Chiled.ondelelte() {
                    @Override
                    public void onDelete(int chiledpos, boolean chiled_flag) {
                        if(mondele != null) {
                            mondele.OnDelete(chiledpos,chiled_flag,position,b);
                        }
                    }
                });
            }
        });
        myAdapter_chiled.setCallBack(new MyAdapter_Chiled.allCheck() {
            @Override
            public void OnCheckLinstener_Chiled(boolean isChecked, int chiledposition) {
                if(mCallBack != null) {
                mCallBack.OnItemCheckLinstener(isChecked,position,chiledposition);
             }
            }
        });
        myAdapter_chiled.setAddjian(new MyAdapter_Chiled.jiajian() {
            @Override
            public void Onadd(int addcount) {
                if(mCallBack != null) {
                mCallBack.Onadd(addcount);
                }
            }
            @Override
            public void Onjian(int jiancount) {
                if(mCallBack != null) {
                 mCallBack.Onjian(jiancount);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if(alist == null) {
            return 0;
        }
        return alist.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private CheckBox father_che;
        private TextView father_title;
        private RecyclerView chiled_recy;
        public ViewHolder(View itemView) {
            super(itemView);
            father_che = itemView.findViewById(R.id.item_father_check);
            father_title = itemView.findViewById(R.id.item_father_shopname);
            chiled_recy = itemView.findViewById(R.id.recy_chiled);
        }
    }
    private allCheck mCallBack;
    public void setCallBack(allCheck CallBack) {
        this.mCallBack = CallBack;
    }
    public interface allCheck {
        void OnCheckLinstener_Father(boolean flag,int position);
        void OnItemCheckLinstener(boolean isSelected,int fatherposition,int chiledsposition);
        void Onadd(int addnNum);
        void Onjian(int jiannNum);
    }
    //delete
    private ondele mondele;
    public void setOnDeleLinstener(ondele ondele) {
        this.mondele = ondele;
    }
    public interface ondele{
        void OnDelete(int chiled_pos,boolean chiled_flag,int father_pos,boolean father_flag);
    }
}
